package cn.kooki.app.duobao.ui.widget.banner;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.data.Bean.Index.BannerItem;


public class SliderBannerController {


    private ArrayList<String> infoList;
    private Context mContenxt;
    private SliderBanner mSliderBanner;

    private InnerAdapter mBannerAdapter = new InnerAdapter();
    private DotView mDotView;


    private BannerItemClickListenter bannerItemClickListenter;

    public void setBannerItemClickListenter(BannerItemClickListenter bannerItemClickListenter) {
        this.bannerItemClickListenter = bannerItemClickListenter;
    }

    public interface BannerItemClickListenter {
        void OnItemClick(View view, int postion);
    }

    private View.OnClickListener mClickItemListener = new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            if (bannerItemClickListenter != null) {
                bannerItemClickListenter.OnItemClick(view, (Integer) view.getTag());
            }

        }
    };

    public SliderBannerController(SliderBanner sliderBanner, Context context) {

        mDotView = (DotView) sliderBanner.findViewById(R.id.ad_indicator);
        mContenxt = context;
        mSliderBanner = sliderBanner;

        sliderBanner.setAdapter(mBannerAdapter);
    }

    public void play(ArrayList<String> list) {
        infoList = list;
        mBannerAdapter.setData(list);
        mBannerAdapter.notifyDataSetChanged();
        mSliderBanner.setDotNum(list.size());

        mSliderBanner.beginPlay();
    }

    public void stop() {

    }

    public void notifyDataSetChanged() {
        mSliderBanner.setDotNum(infoList.size());
        mBannerAdapter.notifyDataSetChanged();
//        mBannerAdapter.updateUI(infoList.size()-1);
    }

    private class InnerAdapter extends BannerAdapter {
        private ArrayList<String> mDataList;

        public void setData(ArrayList<String> data) {
            mDataList = data;
        }

        public String getItem(int position) {
            if (mDataList == null)
                return null;
            return mDataList.get(getPositionForIndicator(position));
        }

        @Override
        public int getPositionForIndicator(int position) {
            if (null == mDataList || mDataList.size() == 0) {
                return 0;
            }
            return position % mDataList.size();
        }
//        public void updateUI(int position){
//            if (position<2){
//                BanerInfo item = getItem(position);
//                View convertView = views.get(position);
//                TextView titleTextView = (TextView) convertView.findViewById(R.id.tv_adcar_number);
//                titleTextView.setText(item.count+"");
//                LinearLayout ly_adtext=(LinearLayout)convertView.findViewById(R.id.ly_numberview);
//                ly_adtext.setVisibility(View.VISIBLE);
//            }else {
//                return;
//            }
//
//        }

        @Override
        public View getView(LayoutInflater layoutInflater, int position) {
            View convertView = layoutInflater.inflate(R.layout.slider_item_layout, null);

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            String item = getItem(position);
            ImageView imageView = (ImageView) convertView.findViewById(R.id.slider_image);
            imageView.setAdjustViewBounds(false);

            Glide.with(mContenxt).load(item).asBitmap().fitCenter().into(imageView);
            convertView.setTag(position);
            convertView.setOnClickListener(mClickItemListener);
            return convertView;
        }

        @Override
        public int getCount() {
            if (mDataList == null) {
                return 0;
            }
            return Integer.MAX_VALUE;
        }
    }
}
